/**
 * 
 */
package com.jushu.storbox.entities;

import java.util.Calendar;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.IBaseDao;
import com.jushu.storbox.database.IFolderDao;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderResponse;

/**
 * Folder info domain object class
 */
public class FolderInfo extends FileFolderInfo{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5079523505768819865L;


	public void valueOf(FolderResponse folderInfo){
		setId(folderInfo.getId());
		setType(folderInfo.getType());
		setName(folderInfo.getName());
		setSize(folderInfo.getSize());
		setStatus(folderInfo.getStatus());
		setCreatedAt(folderInfo.getCreatedAt());
		setModifiedAt(folderInfo.getModifiedAt());
		setOwnerBy(folderInfo.getOwnerBy());
		setCreatedBy(folderInfo.getCreatedBy());
		setModifiedBy(folderInfo.getModifiedBy());
		setContentCreatedAt(folderInfo.getContentCreatedAt());
		setContentModifiedAt(folderInfo.getContentModifiedAt());
		setParent(folderInfo.getParent());
		setShare(folderInfo.getIsShare());
		setSync(false);
		setSharelink(folderInfo.getIsSharelink());
		setEncrypt(folderInfo.getIsEncrypt());
		setIsFile(0);
		setCurrentUserId(ShareDriveApplication.getInstance().getWnerID());
		setOperationTime(Calendar.getInstance().getTimeInMillis());
		setLocalLastModifyTime(Calendar.getInstance().getTimeInMillis());
		setTransStatus(Constant.DOWNLOAD_NON_UPDATE_DB);
		setLastRefreshTime(Calendar.getInstance().getTimeInMillis());
		setContentSyncState(Constant.SYNC_STATUS_TRUE);
	}
	
	/**
	 * <font color="red">current object must come from database(local)</font>
	 * @param folderInfo
	 */
	public void copyFiledValue(FolderResponse folderInfo){
		setId(folderInfo.getId());
		setType(folderInfo.getType());
		setName(folderInfo.getName());
		setSize(folderInfo.getSize());
		setStatus(folderInfo.getStatus());
		setCreatedAt(folderInfo.getCreatedAt());
		setModifiedAt(folderInfo.getModifiedAt());
		setOwnerBy(folderInfo.getOwnerBy());
		setCreatedBy(folderInfo.getCreatedBy());
		setModifiedBy(folderInfo.getModifiedBy());
		setContentCreatedAt(folderInfo.getContentCreatedAt());
		setContentModifiedAt(folderInfo.getContentModifiedAt());
		setParent(folderInfo.getParent());
		setShare(folderInfo.getIsShare());
		setSharelink(folderInfo.getIsSharelink());
		setEncrypt(folderInfo.getIsEncrypt());
		setIsFile(0);
		setCurrentUserId(ShareDriveApplication.getInstance().getWnerID());
		setLastRefreshTime(Calendar.getInstance().getTimeInMillis());
	}

	@Override
	public void valueOf(FileInfoResponseV2 fileInfo) {
		throw new UnsupportedOperationException("folder not support set file infomation!");
	}

	@Override
	public void fromCursor(Cursor cursor) {
		setId(cursor.getString(cursor.getColumnIndex(IBaseDao.id)));
        setName(cursor.getString(cursor.getColumnIndex(IBaseDao.file_name)));
        setIsFile(cursor.getInt(cursor.getColumnIndex(IBaseDao.is_file)));
        setOwnerBy(cursor.getString(cursor.getColumnIndex(IBaseDao.owner_by)));
        setParent(cursor.getString(cursor.getColumnIndex(IBaseDao.parent_folder_id)));
        setSize(cursor.getLong(cursor.getColumnIndex(IBaseDao.file_size)));
        setCreatedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.server_ctime)));
        setModifiedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.server_mtime)));
        setLoctPath(cursor.getString(cursor.getColumnIndex(IBaseDao.local_path)));
        setContentCreatedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.client_ctime)));
        setContentModifiedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.client_mtime)));
        setLastRefreshTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.last_refresh_time)));
        setContentSyncState(cursor.getInt(cursor.getColumnIndex(IBaseDao.content_sync_status)));
        setStatus(cursor.getString(cursor.getColumnIndex(IBaseDao.static_status)));
        setTransStatus(cursor.getInt(cursor.getColumnIndex(IBaseDao.trans_status)));
        setOperationTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.local_operation_time)));
        setLocalLastModifyTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.local_last_modify_time)));
        setDescription(cursor.getString(cursor.getColumnIndex(IBaseDao.description)));
        setEncrypt(cursor.getInt(cursor.getColumnIndex(IBaseDao.isencrypt)) == 1);
        setShare(cursor.getInt(cursor.getColumnIndex(IBaseDao.isshare)) == 1);
        setSharelink(cursor.getInt(cursor.getColumnIndex(IBaseDao.isshare_link)) == 1);
        setIsFile(0);
        setSync(cursor.getInt(cursor.getColumnIndex(IBaseDao.isSync)) == 1);
        setCurrentUserId(cursor.getString(cursor.getColumnIndex(IFolderDao.CURRENT_USER_ID)));
	}

	@Override
	public ContentValues toContentValues() {
		ContentValues cv = super.toContentValues();
		cv.put(IBaseDao.id, getId());
        cv.put(IBaseDao.file_name, getName());
        cv.put(IBaseDao.is_file, 0);
        cv.put(IBaseDao.owner_by, getOwnerBy());
        cv.put(IBaseDao.parent_folder_id, getParent());
        cv.put(IBaseDao.file_size, getSize());
        cv.put(IBaseDao.server_ctime, getCreatedAt());
        cv.put(IBaseDao.server_mtime, getModifiedAt());
        cv.put(IBaseDao.local_path, "");
        cv.put(IBaseDao.client_ctime, getContentCreatedAt());
        cv.put(IBaseDao.client_mtime, getContentModifiedAt());
        cv.put(IBaseDao.last_refresh_time, getLastRefreshTime());
        cv.put(IBaseDao.content_sync_status, getContentSyncState());
        cv.put(IBaseDao.static_status, getStatus());
        cv.put(IBaseDao.trans_status, getTransStatus());
        cv.put(IBaseDao.trans_status_ref_cnt, 0);
        cv.put(IBaseDao.is_exist, 0);
        cv.put(IBaseDao.local_operation_time, Calendar.getInstance().getTimeInMillis());
        cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance().getTimeInMillis());
        cv.put(IBaseDao.description, getDescription());
        cv.put(IBaseDao.isencrypt, isEncrypt());
        cv.put(IBaseDao.isshare, isShare());
        cv.put(IBaseDao.isshare_link, isSharelink());
        cv.put(IBaseDao.isSync, isSync() ? Constant.DOWNLOAD_STATUS_CACHE_DB : Constant.DOWNLOAD_STATUS_NOCACHE_DB);
        cv.put(IFolderDao.CURRENT_USER_ID, ShareDriveApplication.getInstance().getWnerID());
		return cv;
	}
	
	public ContentValues getIdentityCV() {
		throw new RuntimeException("Folder call function getIdentityCV");
	}
}
